package com.example.wanjialei.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.example.wanjialei.pojo.Advisory;
import com.example.wanjialei.pojo.Notice;
import com.example.wanjialei.service.AdvisoryService;
import com.example.wanjialei.service.NoticeService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@CrossOrigin
@RestController
public class NoticeController {
    @Resource
    private NoticeService noticeService;
    @Value("${server.port}")
    private String port;
    private static final String ip = "http://192.168.85.140";

    @RequestMapping("/getNoticeInfo")
    public Map<String,Object> getAdvisoryInfo( Notice notice){
        System.out.println(notice);
        return noticeService.selectAll(notice);
    }

    @RequestMapping("/modifyNoticeInfoById")
    public Map<String,Object> modifyAdvisoryInfoById(@RequestBody Notice notice){
        return noticeService.modifyById(notice);
    }
    @RequestMapping("/deleteNoticeById")
    public Map<String,Object> deleteAdvisoryById(@RequestBody Notice notice){
        return noticeService.deleteById(notice);
    }
    @RequestMapping("/insertNoticeInfo")
    public Map<String,Object> insertAdvisoryInfo(@RequestBody Notice notice){
        return noticeService.insert(notice);
    }
    @RequestMapping("/upload")
    public String fileupload(MultipartFile file, HttpServletRequest req)throws IOException {
        String originalFilename = file.getOriginalFilename();
        String flag = IdUtil.fastSimpleUUID();
        String rootFilePath = System.getProperty("user.dir")+"/project_img/"+flag+"_"+originalFilename;
        FileUtil.writeBytes(file.getBytes(),rootFilePath);
        String url = ip + ":"+ port +"/"+flag;
        return url;

    }
    @RequestMapping("/{flag}")
    public void getFiles(@PathVariable String flag, HttpServletResponse response){
        OutputStream os;
        String basePath = System.getProperty("user.dir")+ "/project_img/";
        List<String> fileNames = FileUtil.listFileNames(basePath);

        String fileName = fileNames.stream().filter(name -> name.contains(flag)).findAny().orElse("");
        try{
            if(StrUtil.isNotEmpty(fileName)){
                response.addHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(fileName,"UTF-8"));
                response.setContentType("application/octet-stream");
                byte[] bytes = FileUtil.readBytes(basePath+fileName);
                os = response.getOutputStream();
                os.write(bytes);
                os.flush();
                os.close();
            }
        }catch (Exception e) {
            System.out.println("文件下载失败");
        }
    }
}
